package com.hrcb.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class SpringDocConfig {

    private static final String basePackage = "com.hrcb.web";//需要扫描api路径
    private static final String headerName = "Authorization";//请求头名称

    @Bean
    public GroupedOpenApi ljGroup() {
        return GroupedOpenApi.builder()
                .group("lj")
//                .addOperationCustomizer((operation, handlerMethod) -> {
//                    operation.addSecurityItem(new SecurityRequirement().addList(headerName));
//                    return operation;
//                })
                .packagesToScan(basePackage)
                .build();
    }

    @Bean
    public OpenAPI createOpenAPI() {
        Components components = new Components();
        //添加右上角的统一安全认证
        components.addSecuritySchemes(headerName,
                new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .scheme("basic")
                        .name(headerName)
                        .in(SecurityScheme.In.HEADER)
                        .description("请求头")
        );

        return new OpenAPI()
                //.components(components)
                .info(apiInfo());
    }

    private Info apiInfo() {
        Contact contact = new Contact();
        contact.setEmail("1234567890@jshmrcb.com");
        contact.setName("海门农商银行");
        contact.setUrl("https://www.jshmrcb.com");

        return new Info()
                .title("廉洁档案接口服务")
                .version("1.0.0")
                //.contact(contact)
                //.description("接口说明")
                .license(new License().name("Apache 2.0").url("http://springdoc.org"));
    }


}